import { compareDate } from '@theme/helpers/utils'

// 过滤博客数据
export function filterPosts (posts, isTimeline) {
  posts = posts.filter((item, index) => {
    const { title, frontmatter: { home, date, publish } } = item
    // 过滤多个分类时产生的重复数据
    if (posts.indexOf(item) !== index) {
      return false
    } else {
      const someConditions = home == true || title == undefined || publish === false
      const boo = isTimeline === true
        ? !(someConditions || date === undefined)
        : !someConditions
      return boo
    }
  })
  return posts
}

// 排序博客数据
export function sortPostsByStickyAndDate (posts) {
  posts.sort((prev, next) => {
    const prevSticky = prev.frontmatter.sticky
    const nextSticky = next.frontmatter.sticky
    if (prevSticky && nextSticky) {
      return prevSticky == nextSticky ? compareDate(prev, next) : (prevSticky - nextSticky)
    } else if (prevSticky && !nextSticky) {
      return -1
    } else if (!prevSticky && nextSticky) {
      return 1
    }
    return compareDate(prev, next)
  })
}

export function sortPostsByDate (posts) {
  posts.sort((prev, next) => {
    return compareDate(prev, next)
  })
}

// 把带有 isHot 标签的文章放到列表的最前面
export function sortPostsByIsHotTag(posts) {
  const isHotTagPostArr = []
  for(let i = 0; i<posts.length; i++){
    let item = posts[i]
    if(item.frontmatter.isHot){
      // markdowm文档 头部(yaml)标记了改篇文章为 热门文章
      item.curIndex = i
      isHotTagPostArr.push(item)
      continue
    }
  }
  const length = isHotTagPostArr.length
  if( length != 0) {
    for(let i = 0; i<length; i++){
      posts.splice(isHotTagPostArr[i].curIndex, 1)
    }
    // 放在页面的最前面
    posts.unshift(...isHotTagPostArr)
  }
}